/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.web.actions.usuarios;

import com.ejemplo.bt.cuentas.services.CuentaServices;
import com.ejemplo.bt.cuentas.vo.Cuenta;
import com.web.actions.AbstractAction;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

/**
 *
 * @author Usuario
 */
public class EliminarCuentaAction extends AbstractAction{

    /* forward name="success" path="" */
    private static final String SUCCESS = "success";

    /**
     * This is the action called from the Struts framework.
     *
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
   public ActionForward execute(ActionMapping actionMapping,
      ActionForm actionForm,
      HttpServletRequest request,
      HttpServletResponse response) {

    ActionErrors errors = new ActionErrors();

    try {

      long idCuenta =
          new Long((String)request.getParameter("idCuenta")).longValue();

      CuentaServices services = new CuentaServices();
      Cuenta c = services.obtenerCuenta(idCuenta);
      if(c.getSaldo() > 0){
          errors.add(ActionErrors.GLOBAL_MESSAGE, new ActionMessage("El saldo de cuenta tiene dinero"));
          return actionMapping.findForward("failure");
      }
      services.eliminarCuenta(idCuenta);

      return actionMapping.findForward("success");
    }
    catch (Exception ex) {
      ex.printStackTrace();
      // Seteo los errores en el request.
      errors.add(ActionErrors.GLOBAL_MESSAGE, new ActionMessage("error.message",
          ex.getMessage()));
      saveErrors(request, errors);

      // Forwardeo.
      return actionMapping.findForward("failure");
    }
  }
}
